<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class AddSysTemplateToCruiseOrderInputsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('cruise_order_inputs', function (Blueprint $table) {
            $table->unsignedTinyInteger('templateType')->default(0)->comment('模板类型')->after('id');
            $table->unsignedSmallInteger('sysTemplate')->default(0)->comment('系统模板')->after('templateType');
            $table->unsignedInteger('orderId')->default(0)->comment('对应业务表id')->change();
        });

        Schema::table('cruise_template', function (Blueprint $table) {
            $table->unsignedSmallInteger('sysTemplate')->default(0)->change();
        });

        \App\Models\Cruise\CruiseOrderInputModel::query()
                                                ->update([
                                                    'sysTemplate'  => \App\Models\Cruise\CruiseTemplateModel::SYS_TEMPLATE_1,
                                                    'templateType' => \App\Models\Cruise\CruiseTemplateModel::TYPE_1
                                                ]);

        \Illuminate\Support\Facades\DB::statement('ALTER TABLE `cruise_order_inputs`
DROP INDEX `idx_orderId`,
ADD UNIQUE `idx_orderId_union`(`orderId` ASC, `sysTemplate`) USING BTREE;');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('cruise_order_inputs', function (Blueprint $table) {
            $table->dropColumn('templateType');
            $table->dropColumn('sysTemplate');
        });
    }
}
